package com.xwb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xwb.mapper.CartoonMapper;
import com.xwb.pojo.Cartoon;
import com.xwb.pojo.Review;

@CrossOrigin
@Controller
public class CartoonController {
	@Autowired
	private CartoonMapper cartoonMapper;
	
	/*
	 * 遍历列表
	 */
	@RequestMapping("/cartoonlist")
	@ResponseBody
	public Map<String, Object> List() {
		Map<String, Object> map = new HashMap<String, Object>();
		ArrayList<Cartoon> cartoon = cartoonMapper.list();
		try {
			map.put("cartoon", cartoon);
		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
		System.out.println("推荐动漫列表遍历成功");
		return map;		
		
	}


	/*
	 * 根据id显示详细内容
	 */
	@RequestMapping("/cartoondetail")
	@ResponseBody
	public Map<String, Object> getObj(@RequestBody Map<String, Integer> requestMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("cartoon", cartoonMapper.findByid(requestMap.get("cid")));
		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
		System.out.println(requestMap.get("cid")+"号内容显示成功");
		return map;
	}

	
	/*
	 * 点赞
	 */
	@RequestMapping("/cartoonwant")
	@ResponseBody
	public void bookwant(@RequestBody Map<String, Integer> requestMap) {
		cartoonMapper.updatawant(requestMap.get("cid"), requestMap.get("cwant"));
		System.out.println("点赞成功");
	}
	
	
	/*
	 * 评论
	 */
	@RequestMapping("/crsend")
	@ResponseBody
	public void rsend(@RequestBody Review review) {
		cartoonMapper.addreview(review);
		System.out.println("评论成功");
	}
	
	/*
	 * 遍历评论
	 */
	@RequestMapping("/crlist")
	@ResponseBody
	public Map<String, Object> rlist(@RequestBody Map<String, Integer> requestMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		ArrayList<Review> review = cartoonMapper.findreviewByid(requestMap.get("cid"));
		try {
			map.put("review", review);
		} catch (Exception e) {
			e.printStackTrace();
			map.clear();
		}
		System.out.println("动漫推荐评论遍历成功");
		return map;			
	}
}
